package XZK010305.net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;

import XZK010305.bean.Express;
import XZK010305.service.ExpressService;

/**
 * <p>
 * 模拟服务器端
 * </p>
 *
 * @author bing.z 2021/6/22 14:30
 **/
public class Server {
    /**
     * 快递E站-操作类
     **/
    private static ExpressService expressService = new ExpressService();

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    /**
     * 服务器端工作主方法
     */
    public void start() {
        new Thread(() -> {
            try (ServerSocket serverSocket = new ServerSocket(10086);) {
                System.out.println("服务器启动成功！");
                // 创建服务端 socket
                while (true) {
                    // 等待客户端连接
                    Socket socket = serverSocket.accept();
                    // 获取输入输出流
                    OutputStream os = socket.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os);
                    InputStream is = socket.getInputStream();
                    ObjectInputStream ois = new ObjectInputStream(is);
                    // 获取到连接后,开一个线程为其工作
                    new Thread(() -> {
                        System.out.println("客户端 " + socket.hashCode() + " 已连接, " + "线程 " + Thread.currentThread().getName() + " 将处理其请求");
                        try {
                            handleRequest(ois, oos);
                        } catch (IOException | ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    /**
     * 处理客户端请求
     *
     * @param ois
     * @param oos
     */
    private void handleRequest(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        while (true) {
            String operation = ois.readUTF();
            if ("adminLogin".equals(operation)) {
                // 获取输入的登录信息
                Object object = ois.readObject();
                if (object instanceof String[] && ((String[]) object).length == 2) {
                    String[] loginMsg = (String[]) object;
                    boolean check = expressService.adminCheck(loginMsg[0], loginMsg[1]);
                    // 将校验结果返回前端
                    oos.writeObject(check);
                } else {
                    oos.writeObject("输入信息有误");
                }
            } else if ("userLogin".equals(operation)) {
                // 获取输入的登录信息
                Object object = ois.readObject();
                if (object instanceof String[] && ((String[]) object).length == 2) {
                    String[] loginMsg = (String[]) object;
                    boolean check = expressService.userCheck(loginMsg[0], loginMsg[1]);
                    // 将校验结果返回前端
                    oos.writeObject(check);
                } else {
                    oos.writeObject("输入信息有误");
                }
            } else if ("exit".equals(operation)) {
                System.out.println("线程 " + Thread.currentThread().getName() + " 完成工作");
                break;
            } else if ("adminOpt".equals(operation)) {
                String text = ois.readUTF();
                if ("findAll".equals(text)) {
                    Map<String, Express> expressMap = expressService.getAll();
                    oos.writeObject(expressMap);
                } else if ("insert".equals(text)) {
                    Express express = (Express) ois.readObject();
                    Express insert = expressService.insert(express);
                    oos.writeObject(insert);
                } else if ("delete".equals(text)) {
                    String number = (String) ois.readObject();
                    boolean delete = expressService.delete(number);
                    oos.writeObject(delete);
                } else if ("update".equals(text)) {
                    String number = (String) ois.readObject();
                    Express oldExpress = expressService.getByNumber(number);
                    oos.writeObject(oldExpress);
                    Express updateExpress = (Express) ois.readObject();
                    Express update = expressService.update(updateExpress);
                    oos.writeObject(update);
                    number = (String) ois.readObject();
                    expressService.delete(number);
                }
            } else if ("userOpt".equals(operation)) {
                String text = ois.readUTF();
                if ("takeExpress".equals(text)) {
                    int code = (int) ois.readObject();
                    Express express = expressService.takeByCode(code);
                    oos.writeObject(express);
                }
            }
        }
    }
}
